package com.example.administrator.szb.activity;

import android.content.pm.ActivityInfo;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.example.administrator.szb.R;
import com.example.administrator.szb.activity.base.BaseActivity;
import com.example.administrator.szb.bean.GWXQBean;
import com.example.administrator.szb.bean.ResultBean;
import com.example.administrator.szb.tinkerutil.SampleApplicationLike;
import com.example.administrator.szb.http.HttpUtil;
import com.example.administrator.szb.http.URLAddress;
import com.example.administrator.szb.util.DialogUtil;
import com.example.administrator.szb.util.GetJsonDataUtil;
import com.example.administrator.szb.util.ScreenExpressionUtils;
import com.example.administrator.szb.util.StatusBarUtil;
import com.example.administrator.szb.util.Toasts;
import com.example.zhouwei.library.CustomPopWindow;
import com.jph.takephoto.app.TakePhotoActivity;

import java.util.ArrayList;
import java.util.HashMap;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by D N on 2017/10/18.
 */

public class JGGLNOActivity extends TakePhotoActivity {
    @Bind(R.id.textView82)
    TextView textView82;
    @Bind(R.id.textView104)
    TextView textView104;
    @Bind(R.id.editText3)
    EditText editText3;
    @Bind(R.id.textView112)
    TextView textView112;
    @Bind(R.id.button5)
    Button button5;
    @Bind(R.id.editText4)
    EditText editText4;
    @Bind(R.id.jiancheng)
    TextView jiancheng;
    String iconPath = "";//从相机或相册中获取的图片路径
    CustomPopWindow popWindow;//弹出框
    ArrayList<String> ming_pian_info_rz = new ArrayList<String>();//存放用户填入的信息
    String city_id = "0";//地址选择的城市id

    GWXQBean gwxqBean;
    private ArrayList<String> jjgl_info = new ArrayList<>();//存放信息
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.jggl_no_activity);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        ButterKnife.bind(this);
        SampleApplicationLike.getActivitiesInstance().add(this);
        StatusBarUtil.setColor(this, R.color.basecolor, 0);
        initToolbar(R.id.jggl_toolbar_include,"机构编辑");
        /*if (FragmentMyForNoRenZhen.userBean.getData().getGenre() == 5){
            jgglTextInfo.setVisibility(View.GONE);
        }*/
        initView();
    }


    /**
     * 初始化toolbar
     * @param
     *
     */
    public void initToolbar(int layout_toolbar,String title){
        /*TextView bar_title = findViewById(R.id.bar_title);
        bar_title.setText("机构编辑");*/
        View toolbar;
        toolbar = findViewById(layout_toolbar);
        ((TextView)toolbar.findViewById(R.id.toolbar_title)).setText(title);
        toolbar.findViewById(R.id.toolbar_fh).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        button5.setOnClickListener(new View.OnClickListener() {
            @RequiresApi(api = Build.VERSION_CODES.KITKAT)
            @Override
            public void onClick(View v) {
                requestUpData();
            }
        });
    }


    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    private void requestUpData() {
        boolean flag = checkInfo();//检查用户信息是否完整；
        if (flag) {
            //doUpData();

            DialogUtil.showPopupwindow(JGGLNOActivity.this, R.layout.popuwindow_tcdl, ScreenExpressionUtils.dip2px(this, 324), ScreenExpressionUtils.dip2px(this, 164), R.layout.sz_activity, new BaseActivity.CallbackPopuwindow() {
                @Override
                public void doSomeThing(View view) {
                    ((TextView) view.findViewById(R.id.textView14)).setText("修改资料");
                    ((TextView) view.findViewById(R.id.textView8)).setText("是否确认提交");

                    ((TextView) view.findViewById(R.id.textView15)).setText("确认提交");
                    ((TextView) view.findViewById(R.id.textView9)).setText("取消");
                    ((TextView) view.findViewById(R.id.textView15)).setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            doUpData();
                            DialogUtil.popWindow.dissmiss();
                        }
                    });

                    ((TextView) view.findViewById(R.id.textView9)).setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            DialogUtil.popWindow.dissmiss();
                        }
                    });
                }
            });
        }
    }

    private void doUpData() {
        DialogUtil.showProgressbar(this,R.layout.jggl_no_activity);
        HashMap<String,String> map = new HashMap<>();
        map.put("code", HttpUtil.getParam());
        map.put("users_id",SampleApplicationLike.getUserloginInstance().getId()+"");
        map.put("logo","0");
        map.put("city_id", gwxqBean.getData().getCity_number());
        map.put("company",gwxqBean.getData().getCompany());
        map.put("work_tel", gwxqBean.getData().getWork_tel());
        map.put("jigou_introduce",editText3.getText().toString().trim());
        map.put("yewu_introduce",editText4.getText().toString().trim());
        String url = URLAddress.URLBASE + URLAddress.URL_JGXG;
        String json = GetJsonDataUtil.simpleMapToJsonStr(map);
        HttpUtil.RequestGsonPost(SampleApplicationLike.getQueueInstance(), Request.Method.POST, ResultBean.class, url, map, new Response.Listener() {
            @Override
            public void onResponse(Object response) {
                ResultBean bean = (ResultBean) response;
                DialogUtil.loading.dissmiss();
                if (bean.getResult() == 1){
                    Toasts.show(JGGLNOActivity.this, "修改成功", Toast.LENGTH_LONG);
                    finish();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                DialogUtil.loading.dissmiss();
                Toasts.show(JGGLNOActivity.this, ""+error.toString(), Toast.LENGTH_SHORT);
            }
        });
    }


    /**
     * 检查信息
     *
     * @return
     */
    private boolean checkInfo() {
        if(TextUtils.isEmpty(editText3.getText().toString())){
            Toasts.show(this,"请输入公司介绍");
            return false;
        }
        if(TextUtils.isEmpty(editText4.getText().toString())){
            Toasts.show(this,"请输入业务介绍");
            return false;
        }
        return true;
    }
    private void initView() {
        gwxqBean = JGGLYESActivity.gwxqBean;

        if (gwxqBean != null){
            textView112.setText(gwxqBean.getData().getCompany());
            textView82.setText(gwxqBean.getData().getWork_tel());
            jiancheng.setText(gwxqBean.getData().getIntro());
            textView104.setText(gwxqBean.getData().getCity_id());
            editText3.setText(gwxqBean.getData().getJigou_introduce());
            editText4.setText(gwxqBean.getData().getYewu_introduce());
        }

    }




}
